package puzzle.common.imgreader;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import tomoto.assertion.AssertUtil;
import tomoto.io.StreamUtil;
import tomoto.util.ArrayUtil;
import tomoto.util.IntVectorUtil;
import tomoto.util.ReadOnlyIterator;

public class ImgPatterns {
	public static final int CHARACTER_PRIORITY = 1;
	public static final int DECORATION_PRIORITY = 2;
	public static final int BACKGROUND_PRIORITY = 3;

	public static final ImgPattern BLOCK;
	public static final ImgPattern KUROMARU;
	public static final ImgPattern SHIROMARU1;
	public static final ImgPattern SHIROMARU2;

	public static final int MAX_NUMBER = 10;
	public static final ImgPattern[] NUMBERS;
	public static final ImgPattern[] INV_NUMBERS;
	public static final ImgPattern[] SLIM_NUMBERS;

	static {
		try {
			int[] blockPixels = new int[14 * 14];
			Arrays.fill(blockPixels, 255);
			BLOCK = new ImgPattern(14, 14, blockPixels, 0.12, BACKGROUND_PRIORITY);

			KUROMARU = loadPattern("kuromaru", 0.15, DECORATION_PRIORITY);
			SHIROMARU1 = loadPattern("shiromaru1", 0.17, DECORATION_PRIORITY);
			SHIROMARU2 = loadPattern("shiromaru2", 0.17, DECORATION_PRIORITY);

			NUMBERS = new ImgPattern[MAX_NUMBER + 1];
			INV_NUMBERS = new ImgPattern[MAX_NUMBER + 1];
			SLIM_NUMBERS = new ImgPattern[MAX_NUMBER + 1];

			loadNumber(0, 0.23, 0.23);
			loadNumber(1, 0.10, 0.12);
			loadNumber(2, 0.20, 0.17);
			loadNumber(3, 0.21, 0.17);
			loadNumber(4, 0.14, 0.14);
			loadNumber(5, 0.12, 0.120);
			loadNumber(6, 0.12, 0.120);
			loadNumber(7, 0.08, 0.100);
			loadNumber(8, 0.10, 0.100);
			loadNumber(9, 0.12, 0.120);
			loadNumber(10, 0.12, 0.120);

		} catch (IOException ex) {
			throw AssertUtil.fail();
		}
	}

	private static ImgPattern loadPattern(String name, double matchFactor, int priority)
			throws IOException {
		final String path = "pattern/";
		final String dir = name + ".txt";
		final List<String> files = StreamUtil.readAllPerLine(new InputStreamReader(
				ImgPatternFactory.class.getResourceAsStream(path + dir)));
		final Iterator<String> filesIter = files.iterator();

		Iterator<InputStream> i = new ReadOnlyIterator<InputStream>() {
			public boolean hasNext() {
				return filesIter.hasNext();
			}

			public InputStream next() {
				return ImgPatternFactory.class.getResourceAsStream(path + filesIter.next());
			}
		};

		return ImgPatternFactory.createFromImages(i, matchFactor, priority);
	}

	private static void loadNumber(int i, double matchFactor, double invMatchFactor)
			throws IOException {
		ImgPattern p1 = loadPattern(String.valueOf(i), matchFactor, CHARACTER_PRIORITY);
		NUMBERS[i] = p1;

		int[] invPixels = ArrayUtil.dup(p1.pixels);
		IntVectorUtil.trans(invPixels, -1, 0xff);
		ImgPattern p2 = new ImgPattern(p1.width, p1.height, invPixels, invMatchFactor,
				CHARACTER_PRIORITY);
		INV_NUMBERS[i] = p2;
		
		ImgPattern p3 = loadPattern("slim" + i, matchFactor, CHARACTER_PRIORITY);
		SLIM_NUMBERS[i] = p3;
	}

}
